package utils;

import member.manager.IMemberManager;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.io.Serializable;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * User: NGUYEN VAN NHAN
 * Date: 9/17/14
 * Time: 2:01 PM
 * To change this template use File | Settings | File Templates.
 */
public class Utils implements Serializable {
    // local lookup
    public static IMemberManager getMemberManager() {
        return (IMemberManager) lookup("Core/MemberManager");
    }

    private static String prefix = "java:global/ProductSocial/";

    public static Object lookup(String jndiName) {
        try {
            return new InitialContext().lookup(prefix + jndiName);
        } catch (NamingException e) {
            throw new RuntimeException(e);
        }
    }
   // remote lookup
   private static Context initialContext;

    private static final String PKG_INTERFACES = "org.jboss.ejb.client.naming";

    public static Context getInitialContext() throws NamingException {
        if (initialContext == null) {
            String JBOSS_CONTEXT = "org.jboss.naming.remote.client.InitialContextFactory";
            Properties props = new Properties();

            props.put(Context.INITIAL_CONTEXT_FACTORY, JBOSS_CONTEXT);
            props.put(Context.PROVIDER_URL, "remote://localhost:4447");
            props.put("jboss.naming.client.ejb.context", "true");
            props.put("jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT", "false");
            props.put(Context.SECURITY_PRINCIPAL, "dever");
            props.put(Context.SECURITY_CREDENTIALS, "User_dever1");
            initialContext = new InitialContext(props);
        }
        return initialContext;
    }

    public static Object remoteLookup(String jndiName) {
        try {
            Context initialContext1 = getInitialContext();
            return initialContext1.lookup(jndiName);
        } catch (NamingException e) {
            throw new RuntimeException(e);
        }
    }

}
